import React from 'react';
import { useRequest, defaultFormatResult } from '@/hooks';
import { getTotalInfo } from './service/interview';
import { usePlatform } from './hooks';
import RecruitEmailPc from './RecruitEmailPc';
import RecruitEmailMobile from './RecruitEmailMobile';

const steps = [
    {
        title: '面试',
        status: 'wait'
    },
    {
        title: '沟通',
        status: 'wait'
    },
    {
        title: '录用',
        status: 'wait'
    },
    {
        title: '入职',
        status: 'wait'
    }
];

export default function RecruitEmail({ location }) {
    const { query } = location;
    const { id } = query;
    const { isMobile } = usePlatform();
    const [current, setCurrent] = React.useState(-1);
    const [entryStatus, setEntryStatus] = React.useState('init'); // 入职状态
    const { data, run: getTotalDesc } = useRequest(() => getTotalInfo(id), {
        ready: !!id,
        refreshDeps: [id],
        ...defaultFormatResult,
        onSuccess: res => {
            if (res && res.linkStep) {
                steps.forEach((currStep, index) => {
                    if (index >= res.linkStep - 1 && index !== 3) {
                        currStep.status = 'wait';
                    } else {
                        currStep.status = 'finish';
                    }
                });
                // 防止切换了step，进去表单，返回以后回到最新的step
                const step = sessionStorage.getItem('__LETTER_STEP__');
                setCurrent(step ? Number(step) : res.linkStep - 1);
            }
        }
    });
    // console.log('🚀 ~ RecruitEmail ~ data', data);

    const props = {
        id,
        data,
        steps,
        current,
        setCurrent,
        getTotalDesc,
        setEntryStatus,
        entryStatus,
        location
    };

    return isMobile ? <RecruitEmailMobile {...props} /> : <RecruitEmailPc {...props} />;
}
